package com.gaoh.modules.base.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import com.gaoh.modules.base.entity.FileInfo;
import com.gaoh.modules.base.service.IFileInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 文件信息
 * @Author: jeecg-boot
 * @Date: 2020-01-06
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "文件信息")
@RestController
@RequestMapping("/base/fileInfo")
public class FileInfoController extends JeecgController<FileInfo, IFileInfoService> {
    @Autowired
    private IFileInfoService fileInfoService;


    /**
     * 分页列表查询
     *
     * @param fileInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "文件信息-分页列表查询")
    @ApiOperation(value = "文件信息-分页列表查询", notes = "文件信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(FileInfo fileInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<FileInfo> queryWrapper = QueryGenerator.initQueryWrapper(fileInfo, req.getParameterMap());
        Page<FileInfo> page = new Page<FileInfo>(pageNo, pageSize);
        IPage<FileInfo> pageList = fileInfoService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param fileInfo
     * @return
     */
    @AutoLog(value = "文件信息-添加")
    @ApiOperation(value = "文件信息-添加", notes = "文件信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FileInfo fileInfo) {
        fileInfoService.save(fileInfo);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param fileInfo
     * @return
     */
    @AutoLog(value = "文件信息-编辑")
    @ApiOperation(value = "文件信息-编辑", notes = "文件信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FileInfo fileInfo) {
        fileInfoService.updateById(fileInfo);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文件信息-通过id删除")
    @ApiOperation(value = "文件信息-通过id删除", notes = "文件信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        fileInfoService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "文件信息-批量删除")
    @ApiOperation(value = "文件信息-批量删除", notes = "文件信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.fileInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文件信息-通过id查询")
    @ApiOperation(value = "文件信息-通过id查询", notes = "文件信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        FileInfo fileInfo = fileInfoService.getById(id);
        return Result.ok(fileInfo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param fileInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FileInfo fileInfo) {
        return super.exportXls(request, fileInfo, FileInfo.class, "文件信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FileInfo.class);
    }


    /**
     * 文件上传
     * @param request
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public Result<?> uploadImg(HttpServletRequest request, @RequestPart(value = "file", required = false)
            MultipartFile[] multipartFile) {
        Result<String> result = new Result<>();

        try {
            result.setResult(fileInfoService.uploadImg(request, null));
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
